OS := $(shell uname)
ifeq ($(OS),Darwin)
DLEXT := dylib
else
DLEXT := so
endif

ROOT := $(realpath ../../..)
CEXTS_DIR := $(ROOT)/lib/mri
RUBY := $(ROOT)/bin/truffleruby

TRUFFLE_POSIX := $(ROOT)/lib/cext/truffleposix.$(DLEXT)
TRUFFLE_MOCK := $(ROOT)/lib/cext/sulongmock.bc
RUBY_LIBRARY := $(ROOT)/lib/cext/ruby.su

RUBY_HEADERS := $(wildcard $(ROOT)/lib/cext/include/*.h) $(wildcard $(ROOT)/lib/cext/include/ruby/*.h)
RBCONFIG := $(ROOT)/lib/truffle/rbconfig.rb
RBCONFIG_MKMF := $(ROOT)/lib/truffle/rbconfig-for-mkmf.rb
PREPROCESS := $(ROOT)/lib/cext/preprocess.rb $(wildcard $(ROOT)/lib/cext/patches/*.rb)
MKMF := $(ROOT)/lib/mri/mkmf.rb
BASIC_EXTCONF_DEPS := $(TRUFFLE_POSIX) $(RUBY_HEADERS) $(RBCONFIG) $(RBCONFIG_MKMF) $(PREPROCESS) $(MKMF)
# These 2 libraries are needed for try_link() in extconf.rb
EXTCONF_DEPS := $(BASIC_EXTCONF_DEPS) $(TRUFFLE_MOCK) $(RUBY_LIBRARY)

ifeq ($(TRUFFLERUBY_CEXT_ENABLED),false)
goal: no_cexts $(TRUFFLE_POSIX)
else
goal: all
endif

no_cexts: clean_cexts
	@echo "WARNING: TRUFFLERUBY_CEXT_ENABLED is set to false - not building cexts" 1>&2

all: $(RUBY_LIBRARY) $(CEXTS_DIR)/openssl.su $(CEXTS_DIR)/zlib.su $(CEXTS_DIR)/psych.su $(CEXTS_DIR)/syslog.su

clean: clean_cexts clean_truffleposix

clean_truffleposix:
	rm -f $(TRUFFLE_POSIX)
	rm -f truffleposix/*.o truffleposix/truffleposix.$(DLEXT)

clean_cexts:
	rm -f sulongmock/Makefile sulongmock/*.bc sulongmock/sulongmock.su
	rm -f $(ROOT)/lib/cext/sulongmock.bc
	rm -f $(RUBY_LIBRARY)
	rm -f $(ROOT)/lib/cext/ruby.bc
	rm -f cext/Makefile cext/*.bc cext/ruby.su
	rm -f $(CEXTS_DIR)/openssl.su
	rm -f openssl/Makefile openssl/*.bc openssl/openssl.su
	rm -f $(CEXTS_DIR)/zlib.su
	rm -f zlib/Makefile zlib/*.bc zlib/zlib.su
	rm -f $(CEXTS_DIR)/psych.su
	rm -f psych/Makefile psych/*.bc psych/yaml/*.bc psych/psych.su
	rm -f $(CEXTS_DIR)/syslog.su
	rm -f syslog/Makefile syslog/*.bc syslog/syslog.su

# truffleposix
truffleposix/truffleposix.$(DLEXT): truffleposix/Makefile truffleposix/truffleposix.c
	cd truffleposix && $(MAKE)

$(TRUFFLE_POSIX): truffleposix/truffleposix.$(DLEXT)
	cp $< $@

# sulongmock.bc
sulongmock/Makefile: sulongmock/extconf.rb $(BASIC_EXTCONF_DEPS)
	cd sulongmock && $(RUBY) extconf.rb

sulongmock/sulongmock.bc: sulongmock/Makefile sulongmock/sulongmock.c
	cd sulongmock && $(MAKE)

$(ROOT)/lib/cext/sulongmock.bc: sulongmock/sulongmock.bc
	cp $< $@

# ruby.su
cext/Makefile: cext/extconf.rb $(BASIC_EXTCONF_DEPS)
	cd cext && $(RUBY) extconf.rb

cext/ruby.su: cext/Makefile cext/ruby.c
	cd cext && $(MAKE)

$(RUBY_LIBRARY): cext/ruby.su
	cp $< $@
	cp cext/ruby.bc $(ROOT)/lib/cext/ruby.bc

# openssl.su
openssl/Makefile: openssl/extconf.rb $(EXTCONF_DEPS)
	cd openssl && $(RUBY) extconf.rb

openssl/openssl.su: openssl/Makefile openssl/*.c openssl/*.h
	cd openssl && $(MAKE)

$(CEXTS_DIR)/openssl.su: openssl/openssl.su
	cp $< $@

# zlib.su
zlib/Makefile: zlib/extconf.rb $(EXTCONF_DEPS)
	cd zlib && $(RUBY) extconf.rb

zlib/zlib.su: zlib/Makefile zlib/zlib.c
	cd zlib && $(MAKE)

$(CEXTS_DIR)/zlib.su: zlib/zlib.su
	cp $< $@

# syslog.su
syslog/Makefile: syslog/extconf.rb $(EXTCONF_DEPS)
	cd syslog && $(RUBY) extconf.rb

syslog/syslog.su: syslog/Makefile syslog/syslog.c
	cd syslog && $(MAKE)

$(CEXTS_DIR)/syslog.su: syslog/syslog.su
	cp $< $@

# psych.su
psych/Makefile: psych/extconf.rb $(EXTCONF_DEPS)
	# TODO CS 6-Apr-18 --enable-bundled-libyaml should be automatic - the header and symbol check pass and I can't see why
	cd psych && $(RUBY) extconf.rb --enable-bundled-libyaml

psych/psych.su: psych/Makefile psych/*.c psych/*.h psych/yaml/*.c psych/yaml/*.h
	cd psych && $(MAKE)

$(CEXTS_DIR)/psych.su: psych/psych.su
	cp $< $@
